package com.applink.sell_proof.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.applink.sell_proof.database.table.AnalyticDBHelper;
import com.applink.sell_proof.database.table.ClaimDBHelper;
import com.applink.sell_proof.database.table.CountryDBHelper;
import com.applink.sell_proof.database.table.DealDBHelper;
import com.applink.sell_proof.database.table.DistributorDBHelper;
import com.applink.sell_proof.database.table.ImageDBHelper;
import com.applink.sell_proof.database.table.PriceBookDBHelper;
import com.applink.sell_proof.database.table.ProductImageDBHelper;
import com.applink.sell_proof.database.table.ProductModelDBHelper;
import com.applink.sell_proof.database.table.ProjectDBHelper;
import com.applink.sell_proof.database.table.PromotionDBHelper;
import com.applink.sell_proof.database.table.QuestionDBHelper;
import com.applink.sell_proof.database.table.QuizDBHelper;
import com.applink.sell_proof.database.table.RedeemDBHelper;
import com.applink.sell_proof.database.table.RewardClaimDBHelper;
import com.applink.sell_proof.database.table.RewardClaimDetailDBHelper;
import com.applink.sell_proof.database.table.RewardDBHelper;
import com.applink.sell_proof.database.table.RewardProjectDBHelper;
import com.applink.sell_proof.database.table.RewardProjectDetailDBHelper;
import com.applink.sell_proof.database.table.TipDBHelper;
import com.applink.sell_proof.database.table.UserSettingDBHelper;

public class EMarketingDatabaseHelper extends SQLiteOpenHelper {

	public static final String DB_NAME = "emarketing.db";
	private static final int DB_VERSION = 10;

	public static final String[] TABLE_NAMES = new String[] {
			DealDBHelper.TABLE_NAME, DistributorDBHelper.TABLE_NAME,
			CountryDBHelper.TABLE_NAME, TipDBHelper.TABLE_NAME,
			QuestionDBHelper.TABLE_NAME, QuizDBHelper.TABLE_NAME,
			RedeemDBHelper.TABLE_NAME, PriceBookDBHelper.TABLE_NAME,
			AnalyticDBHelper.TABLE_NAME, ProjectDBHelper.TABLE_NAME,
			ClaimDBHelper.TABLE_NAME, PromotionDBHelper.TABLE_NAME,
			ImageDBHelper.TABLE_NAME, ProductModelDBHelper.TABLE_NAME,
			RewardDBHelper.TABLE_NAME, RewardProjectDBHelper.TABLE_NAME,
			RewardProjectDetailDBHelper.TABLE_NAME,
			RewardClaimDBHelper.TABLE_NAME,
			RewardClaimDetailDBHelper.TABLE_NAME,
			ProductImageDBHelper.TABLE_NAME, UserSettingDBHelper.TABLE_NAME };

	public static final String[] SQL_EXCUTE = new String[] {
			DealDBHelper.SQL_EXCUTE, DistributorDBHelper.SQL_EXCUTE,
			CountryDBHelper.SQL_EXCUTE, TipDBHelper.SQL_EXCUTE,
			QuestionDBHelper.SQL_EXCUTE, QuizDBHelper.SQL_EXCUTE,
			RedeemDBHelper.SQL_EXCUTE, PriceBookDBHelper.SQL_EXCUTE,
			AnalyticDBHelper.SQL_EXCUTE, ProjectDBHelper.SQL_EXCUTE,
			ClaimDBHelper.SQL_EXCUTE, PromotionDBHelper.SQL_EXCUTE,
			ImageDBHelper.SQL_EXCUTE, ProductModelDBHelper.SQL_EXCUTE,
			RewardDBHelper.SQL_EXCUTE, RewardProjectDBHelper.SQL_EXCUTE,
			RewardClaimDBHelper.SQL_EXCUTE,
			RewardProjectDetailDBHelper.SQL_EXCUTE,
			RewardClaimDetailDBHelper.SQL_EXCUTE,
			ProductImageDBHelper.SQL_EXCUTE, UserSettingDBHelper.SQL_EXCUTE };

	public EMarketingDatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion < 3) {

			String alterTable = "ALTER TABLE %s ADD COLUMN %s DATETIME;";
			String updateTable = "UPDATE %s SET %s=datetime('1970-01-01 08:48:34');";
			db.execSQL(String.format(alterTable, PromotionDBHelper.TABLE_NAME,
					PromotionDBHelper.PM_DATETIME_SAVED));

			db.execSQL(String.format(alterTable, PriceBookDBHelper.TABLE_NAME,
					PriceBookDBHelper.PB_DATETIME_SAVED));

			db.execSQL(String.format(updateTable, PromotionDBHelper.TABLE_NAME,
					PromotionDBHelper.PM_DATETIME_SAVED));

			db.execSQL(String.format(updateTable, PriceBookDBHelper.TABLE_NAME,
					PriceBookDBHelper.PB_DATETIME_SAVED));
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.beginTransaction();
		try {
			execMultipleSQL(db, SQL_EXCUTE);
			db.setTransactionSuccessful();
		} catch (android.database.SQLException e) {
			throw e;
		} finally {
			db.endTransaction();
		}
	}

	private void execMultipleSQL(SQLiteDatabase db, String[] sql)
			throws android.database.SQLException {
		for (String s : sql) {
			if (s.trim().length() > 0) {
				try {
					db.execSQL(s);
				} catch (android.database.SQLException e) {
					Log.e("EMarketingDB", e.toString());
					throw new android.database.SQLException();
				}
			}
		}
	}

	private void dropMultipleSQL(SQLiteDatabase db, String[] tablename)
			throws android.database.SQLException {
		for (String s : tablename) {
			if (s.trim().length() > 0) {
				try {
					db.execSQL("DROP TABLE IF EXISTS " + s + ";");
				} catch (android.database.SQLException e) {
					throw new android.database.SQLException();
				}
			}
		}
	}

}
